var tableElem = "LAY-home-downloadCategory-list";
var formElem = "LAY-home-downloadCategory-listform";
var tableToolsElem = "LAY-home-downloadCategory-tools";
var formSubmitElem = "LAY-home-downloadCategory-listform-submit";
var formUrl = '/home/downloadCategory/listform'
var historyUrl = '/home/downloadCategory/list'
var baseUrl = window.globalConfig.url_download_category

layui.use(['common'], function (common) {
    var $ = layui.$
        , admin = layui.admin
        , view = layui.view
        , table = layui.table
        , form = layui.form
        , dropdown = layui.dropdown
        , treeTable = layui.treeTable

    ;

    //列表管理
    var tableItem = treeTable.render({
        elem: `#${tableElem}`
        , url: baseUrl //模拟接口
        , lineStyle: 'height: 50px;'
        , tree: { // treeTable 特定属性集
            customName: {
                name: "cat_name",
                id: "cat_id",
                pid: "parent_id"
            },
            data: {},
            view: {},
            async: {},
            callback: {}
        }
        , cols:
            [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'cat_id', width: 100, title: 'id', fixed: 'left', sort: true}
                , {field: 'cat_name', title: '分类名称', minWidth: 250, edit: 'text'}
                , {field: 'urlrewriter', title: '自定义路由', minWidth: 250, edit: 'text'}
                , {field: 'is_show', title: '是否显示', templet: "#ID-table-downloadCategory-is_show-switch"}
                , {field: 'sort_order', title: '排序', edit: 'text'}
                , {title: '操作', minWidth: 380, align: 'center', fixed: 'right', toolbar: `#${tableToolsElem}`}
            ]]
        , page:
            true
        , limit:
            10
        , limits:
            [10, 15, 20, 25, 30]
        , text:
            {
                none: '暂无数据！'
            }

    })
    ;

    var active = {
        //添加
        add: function (othis) {
            location.hash = formUrl
        }
    };

    $('.layui-btn.layuiadmin-btn-list').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    // 增删改查
    table.on(`tool(${tableElem})`, function (obj) {
        var data = obj.data;
        if (obj.event === 'del') { // 删除列表
            layer.confirm('确定删除此列表？', function (index) {
                admin.req({
                    url: baseUrl + "/" + data.cat_id,
                    type: "DELETE",
                    success: function (res) {
                        common.ctmMsg(res.code, res.msg)
                        layer.close(index);
                        tableItem.reloadData(); //重载表格
                    }
                })
            });
        } else if (obj.event === 'edit') {// 修改列表
            location.hash = formUrl + "/cat_id=" + data.cat_id
        } else if (obj.event === 'add') {
            location.hash = formUrl + "/parent_id=" + data.cat_id
        }
    });

    /**
     * 表单回调
     * @param d
     */
    layui.data.done = function (d) {
        var remoteData = d.data;
        form.render();
        // 加载上传插件
        common.uploader('ID-upload-downloadCategory-btn', 'ID-upload-downloadCategory-img', 'photo')

        common.formDropdownEditor(
            window.globalConfig.url_navigate_list,
            'navName',
            'navId',
            'navName'
        )

        // 添加分类
        form.on(`submit(${formSubmitElem})`, (data) => {
            var field = data.field; //获取提交的字段
            field.is_show = field.is_show === undefined ? 0 : field.is_show;
            let url = baseUrl;
            let type = "POST";
            if (remoteData && remoteData.cat_id) {
                url += "/" + remoteData.cat_id;
                type = "PUT"
            }

            //提交 Ajax 成功后，关闭当前弹层并重载表格
            admin.req({
                url: url,
                type: type,
                data: field,
                success: function (res) {
                    if (res.code === 0) {
                        history.back()
                    }
                    common.ctmMsg(res.code, res.msg)
                }
            })
        })


    }

    // 表格文本修改
    common.tableTextEditor(tableElem, baseUrl, 'cat_id')
    common.tableSwitchEditor('downloadCategory-is_show-status', baseUrl)
});